package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.UserDto;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author Jiang
 * @date 2022/2/16
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User loginOrRegister(UserDto user) {
        LambdaQueryWrapper<User> userWrapper = Wrappers.lambdaQuery(User.class);
        userWrapper.eq(User::getPhone, user.getPhone());
        //查数据库，看应手机号是否存在
        //获取到登录用户 进行操作
        User login = userMapper.selectOne(userWrapper);
        if (login == null) {
            login = new User();
            //不存在 => 自动注册
            login.setPhone(user.getPhone());
            login.setName("reggie_u_" + ValidateCodeUtils.generateValidateCode4String(6));
            login.setStatus(1);
            userMapper.insert(login);
        }
        //存在 => 登录成功，存储登录信息
        return login;
    }
}
